package com.firefly.admin.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.firefly.admin.common.vo.Weather;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class WeatherUtil {
    private static final Map<String, String> typeMap = new HashMap<>();

    private final static String PROVINCE_PATH = "http://www.weather.com.cn/data/city3jdata/china.html";
    private final static String CITY_PATH = "http://www.weather.com.cn/data/city3jdata/provshi/%s.html";
    private final static String REGION_PATH = "http://www.weather.com.cn/data/city3jdata/station/%s.html";
    private final static String WEATHER_PATH = "http://t.weather.itboy.net/api/weather/city/%s";

    static {
        typeMap.put("晴", "qing");
        typeMap.put("阴", "yinTian");
        typeMap.put("多云", "duoYun");
        typeMap.put("雾", "wu");
        typeMap.put("小雨", "xiaoYu");
        typeMap.put("中雨", "zhongYu");
        typeMap.put("大雨", "daYu");
        typeMap.put("冻雨", "dongYu");
        typeMap.put("暴雨", "baoYu");
        typeMap.put("雷阵雨", "leiZhenYu");
        typeMap.put("冰雹", "bingBao");
        typeMap.put("雨夹雪", "yuJiaXue");
        typeMap.put("小雪", "xiaoXue");
        typeMap.put("中雪", "zhongXue");
        typeMap.put("大暴雪", "baoXue");
        typeMap.put("霜冻", "shuangDong");
        typeMap.put("霾", "mai");
    }

    public static Weather getWeather(String provinceName, String cityName, String region) {
        String provinceId = getId(PROVINCE_PATH, provinceName);
        if (StringUtils.isBlank(provinceId)) {
            return null;
        }
        String cityId = getId(String.format(CITY_PATH, provinceId), cityName);
        if (StringUtils.isBlank(cityId)) {
            return null;
        }
        String regionId = getId(String.format(REGION_PATH, provinceId + cityId), region);
        if (StringUtils.isBlank(regionId)) {
            return null;
        }
        if (cityId.equals("00")) {
            return getWeather(provinceId + regionId + cityId);
        }
        return getWeather(provinceId + cityId + regionId);
    }

    public static Weather getWeather(String cityId) {
        Weather weather = new Weather();
        try {
            URL url = new URL(String.format(WEATHER_PATH, cityId));
            InputStreamReader isReader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isReader);//采用缓冲式读入
            String str;
            while ((str = br.readLine()) != null) {
                JSONObject jsonObject = JSONObject.parseObject(str);
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray forecast = data.getJSONArray("forecast");
                JSONObject forecastInfo = forecast.getJSONObject(0);
                String low = forecastInfo.getString("low");
                String high = forecastInfo.getString("high");
                String type = forecastInfo.getString("type");
                String notice = "今日" + type + "，" + low + "~" + high + "，" + forecastInfo.getString("notice");
                weather.setWeatherIcon(getWeatherTypeIcon(type));
                weather.setNotice(notice);
            }
            br.close();//网上资源使用结束后，数据流及时关闭
            isReader.close();
        } catch (Exception exp) {
            System.out.println(exp.getMessage());
        }
        return weather;
    }

    public static String getId(String path, String value) {
        try {
            URL url = new URL(path);
            InputStreamReader isReader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isReader);
            String str;
            while ((str = br.readLine()) != null) {
                Map mapType = JSONObject.parseObject(str, Map.class);
                for (Object key : mapType.keySet()) {
                    if (mapType.get(key).equals(value)) {
                        return (String) key;
                    }
                }
            }
            br.close();
            isReader.close();
        } catch (Exception exp) {
            System.out.println(exp.getMessage());
        }
        return "";
    }

    public static String getWeatherTypeIcon(String type) {
        String typeIcon = typeMap.get(type);
        if (StringUtils.isNotBlank(typeIcon)) {
            return typeIcon;
        }
        return "";
    }
}
